package com.warm.pump.module.sys.controller;

import com.warm.pump.frame.gen.util.MyBeanUtils;
import com.warm.pump.frame.gen.util.OutJson;
import com.warm.pump.frame.util.PageUtils;
import com.warm.pump.module.sys.bean.SysRoleDto;
import com.warm.pump.module.sys.bean.po.gen.SysRole;
import com.warm.pump.module.sys.service.ISysRoleMenuService;
import com.warm.pump.module.sys.service.ISysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController {
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    /**
     * 角色列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:role:list")
    public OutJson list(Integer page, Integer limit) {
        Map<String, Object> map = new HashMap<>();
        map.put("offset", (page - 1) * limit);
        map.put("limit", limit);

        // 查询列表数据
        List<SysRole> list = sysRoleService.queryList(map);
        int total = sysRoleService.queryTotal(map);

        PageUtils pageUtil = new PageUtils(list, total, limit, page);

        return OutJson.ok().put("page", pageUtil);
    }

    /**
     * 角色列表
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:role:select")
    public OutJson select() {
        // 查询列表数据
        List<SysRole> list = sysRoleService.queryList(new HashMap<String, Object>());

        return OutJson.ok().put("list", list);
    }

    /**
     * 角色信息
     */
    @RequestMapping("/info/{roleId}")
    @RequiresPermissions("sys:role:info")
    public OutJson info(@PathVariable("roleId") Long roleId) {
        SysRoleDto role = new SysRoleDto();
        SysRole sysRole = sysRoleService.queryObject(roleId);

        try {
            MyBeanUtils.copyBean2Bean(role, sysRole);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 查询角色对应的菜单
        List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
        role.setMenuIdList(menuIdList);

        return OutJson.ok().put("role", role);
    }

    /**
     * 保存角色
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:role:save")
    public OutJson save(@RequestBody SysRoleDto role) {
        if (StringUtils.isBlank(role.getRoleName())) {
            return OutJson.error("角色名称不能为空");
        }

        sysRoleService.save(role);

        return OutJson.ok();
    }

    /**
     * 修改角色
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:role:update")
    public OutJson update(@RequestBody SysRoleDto role) {
        if (StringUtils.isBlank(role.getRoleName())) {
            return OutJson.error("角色名称不能为空");
        }

        sysRoleService.update(role);

        return OutJson.ok();
    }

    /**
     * 删除角色
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:role:delete")
    public OutJson delete(@RequestBody Long[] roleIds) {
        sysRoleService.deleteBatch(roleIds);

        return OutJson.ok();
    }
}
